package com.cdy.grpcserver.service

import com.cdy.grpc.*
import io.grpc.stub.StreamObserver
import net.devh.springboot.autoconfigure.grpc.server.GrpcService

@GrpcService(StudentServiceGrpc::class)
class StudentService : StudentServiceGrpc.StudentServiceImplBase() {

    override fun insertStudent(request: Student?, responseObserver: StreamObserver<BooleanReply>?) {
        super.insertStudent(request, responseObserver)
    }

    override fun getStudent(request: Request?, responseObserver: StreamObserver<Student>?) {
        val student = Student.newBuilder().setId(1)
                .setName(request?.name)
                .setAge(18)
                .build()

        responseObserver?.onNext(student)
        responseObserver?.onCompleted()
    }

    override fun getSameStudentList(request: Request, responseObsever: StreamObserver<Student>) {
        val list = listOf(Student.newBuilder().setId(1).setName("小明").setAge(15).build(),
                Student.newBuilder().setId(2).setName("小红").setAge(15).build(),
                Student.newBuilder().setId(3).setName("小绿").setAge(15).build())

        list.forEach {
            if (it.name == request.name) {
                responseObsever.onNext(it)
            }
        }
        responseObsever.onCompleted()
    }

    override fun getStudentRecord(responseObserver: StreamObserver<Response>): StreamObserver<Request> {
        return object : StreamObserver<Request> {
            var list: ArrayList<Student> = ArrayList()
            var id = 1
            var sum = 0;
            var age = 15

            override fun onNext(value: Request?) {
                ++id
                ++sum
                ++age
                val name = value?.name
                list.add(Student.newBuilder()
                        .setId(id)
                        .setName(name)
                        .setAge(age)
                        .build())
            }

            override fun onError(t: Throwable?) {
                t?.printStackTrace()
            }

            override fun onCompleted() {
                responseObserver.onNext(Response.newBuilder()
                        .setSum(sum)
                        .addAllStudent(list)
                        .build())
                responseObserver.onCompleted()

            }

        }
    }

    override fun getStudentRoster(responseObserver: StreamObserver<Roster>): StreamObserver<Request> {
        return object : StreamObserver<Request> {
          var roster:Roster?=null

            override fun onNext(value: Request?) {
                when(value?.name){
                    "小明"-> {
                        roster = Roster.newBuilder()
                                .setStudentName(value.name)
                                .addTeacher(Teacher.newBuilder().setId(1).setSubject("数学").setName("王老师"))
                                .addTeacher(Teacher.newBuilder().setId(1).setSubject("语文").setName("刘老师"))
                                .addTeacher(Teacher.newBuilder().setId(1).setSubject("英语").setName("李老师"))
                                .build()
                    }
                    "小李"->{
                        roster = Roster.newBuilder()
                                .setStudentName(value.name)
                                .addTeacher(Teacher.newBuilder().setId(1).setSubject("数学").setName("王老师"))
                                .addTeacher(Teacher.newBuilder().setId(1).setSubject("语文").setName("刘老师"))
                                .build()
                    }
                    else ->{
                        roster = Roster.newBuilder()
                                .setStudentName(value?.name)
                                .addTeacher(Teacher.newBuilder().setId(1).setSubject("数学").setName("王老师"))
                                .build()
                    }
                }
                responseObserver.onNext(roster)
            }

            override fun onError(t: Throwable?) {
                t?.printStackTrace()
            }

            override fun onCompleted() {
                responseObserver.onCompleted()
            }

        }
    }
}